// 版权归陈超所有


#include "AbilitySystem/CC_AbilitySystemComponent.h"

#include "AbilitySystem/GameplayAbilities/CC_GameplayAbility.h"

void UCC_AbilitySystemComponent::AbilityActorInfoSet()
{
	//多播委托,绑定回调函数
	OnGameplayEffectAppliedDelegateToSelf.AddUObject(this, &UCC_AbilitySystemComponent::ClientEffectApplied);
}

void UCC_AbilitySystemComponent::ClientEffectApplied_Implementation(UAbilitySystemComponent* AbilitySystemComponent,
	const FGameplayEffectSpec& EffectSpec, FActiveGameplayEffectHandle ActiveEffectHandle)
{
	FGameplayTagContainer TagContainer;
	EffectSpec.GetAllAssetTags(TagContainer);	//获取所有资产标签
	
	OnEffectAssetTags.Broadcast(TagContainer);		//广播一个多播委托
}

void UCC_AbilitySystemComponent::AddCharacterAbilities(const TArray<TSubclassOf<UGameplayAbility>>& StartupAbilities)
{
	for (const TSubclassOf<UGameplayAbility>& AbilityClass : StartupAbilities)
	{
		FGameplayAbilitySpec AbilitySpec = FGameplayAbilitySpec(AbilityClass, 1);
		UCC_GameplayAbility* Ability = Cast<UCC_GameplayAbility>(AbilitySpec.Ability);
		if (Ability == nullptr) continue;
		AbilitySpec.DynamicAbilityTags.AddTag(Ability->StartupInputTag);	//添加动态标签
		
		GiveAbility(AbilitySpec);
	}
}

void UCC_AbilitySystemComponent::AbilityInputTagHeld(const FGameplayTag& InputTag)
{
	if (!InputTag.IsValid()) return;
	for (FGameplayAbilitySpec& AbilitySpec : GetActivatableAbilities())
	{
		if (!AbilitySpec.DynamicAbilityTags.HasTagExact(InputTag)) continue;

		AbilitySpecInputPressed(AbilitySpec);		//通知输入按下了
		if (!AbilitySpec.IsActive())
		{
			TryActivateAbility(AbilitySpec.Handle);		
		}
	}
}

void UCC_AbilitySystemComponent::AbilityInputTagReleased(const FGameplayTag& InputTag)
{
	if (!InputTag.IsValid()) return;
	for (FGameplayAbilitySpec& AbilitySpec : GetActivatableAbilities())
	{
		if (!AbilitySpec.DynamicAbilityTags.HasTagExact(InputTag)) continue;

		AbilitySpecInputReleased(AbilitySpec);		//通知输入释放了
	}
}
